var express = require('express');
var router = express.Router();
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.redirect('manager');
});

router.get('/manager', function(req,res) {
  res.render('manager');
})

router.get('/login', function(req, res) {
  res.render('login', { title: '登录'});
});

router.post('/loginIn', function(req, res, next) {
  if (req.body.username === "admin" && req.body.password=="A123456") {
    var user = {'username':'admin'};
    req.session.user = user;
    res.redirect('/manager');
  } else if (req.body.username === "admin" && req.body.password !== "A123456") {
    res.redirect('/login')
  } else if (req.body.username) {
    var userName = req.body.username;
    var user = {'username':userName};
    req.session.user = user;
    res.redirect('/manager');
  }
});

router.get('/logout', function(req, res){
  req.session.reset();
  res.redirect('/manager');
});

module.exports = router;